﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Impl;
using IntelligentAgriculture.Domain.Interface;
using IntelligentAgriculture.Domain.Interface.IColumnManagement;
using IntelligentAgriculture.Domain.Interface.Iridgepletype;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class DeliveryPromptModelService: IDeliveryPromptModelService
    {
        private readonly IDeliveryPromptModelRepository _deliveryPromptModelRepository;
        private readonly IridegRepository _iridegRepository;
        private readonly IColumnManRepository _columnManRepository;
        private readonly IPregnancyRepository _pregnancyRepository;
        private readonly IBreedingInfoRepository _breedingInfoRepository;
        private readonly IHistoryRecordRepository _historyRecordRepository;
        private readonly AgricultureDbContext _context;
        private readonly IMapper _mapper;
        public DeliveryPromptModelService(IDeliveryPromptModelRepository deliveryPromptModelRepository, IridegRepository iridegRepository, IColumnManRepository columnManRepository, IPregnancyRepository pregnancyRepository, IBreedingInfoRepository breedingInfoRepository, IHistoryRecordRepository historyRecordRepository, AgricultureDbContext context, IMapper mapper)
        {
            _deliveryPromptModelRepository = deliveryPromptModelRepository;
            _columnManRepository = columnManRepository;
            _iridegRepository = iridegRepository;
            _pregnancyRepository = pregnancyRepository;
            _breedingInfoRepository = breedingInfoRepository;
            _historyRecordRepository = historyRecordRepository;
            _context = context;
            _mapper = mapper;
        }
        /// <summary>
        /// 分娩提示列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<DeliverypromptmodelOutput>> GetDeliveryPromptModel(int pageIndex, int pageSize)
        {
            try
            {
                
                var deliveryPromptModels = await _deliveryPromptModelRepository.GetDeliverypromptmodels();
                var buildings = _iridegRepository.Showinfo();
                var columns = _columnManRepository.GetShow();
                var pregnancies = await _pregnancyRepository.GetSheeppregnancy();
                var breedingInfos = await _breedingInfoRepository.GetBreedinginfoAsync();
                var historyRecords = await _historyRecordRepository.GetHistoryRecordsAsync();
                var deliveryQuery = (from delivery in deliveryPromptModels
                                     join eartags in _context.Eartags on delivery.EarTag equals eartags.EarTagNumber
                                     join ridgepole in buildings on delivery.CurrentBuilding equals ridgepole.RidgepoleId
                                     join column in columns on delivery.CurrentField equals column.ColumnManagementId
                                     
                                     select new DeliverypromptmodelOutput
                                     {
                                         DeliveryPromptId = delivery.DeliveryPromptId,
                                         EarTag = delivery.EarTag,
                                         HybridizationDate = delivery.HybridizationDate,
                                         IncidentDate = delivery.IncidentDate,
                                         PrelaborDate = delivery.PrelaborDate,
                                         PromptType = delivery.PromptType,
                                         State = delivery.State,
                                         StateDays = delivery.StateDays, // 妊娠天数
                                         Type = delivery.Type,
                                         CurrentBuilding = ridgepole.RidgepoleId, // 当前栋舍
                                         BuildingName = buildings.FirstOrDefault(c => c.RidgepoleId == delivery.CurrentBuilding).RidgepoleName,
                                         CurrentField = column.ColumnManagementId, // 当前栏位
                                         ColumnName = columns.FirstOrDefault(c => c.ColumnManagementId == delivery.CurrentField).ColumnName
                                     });

                // 构造分页输出
                var queryResult = new PageOutput<DeliverypromptmodelOutput>
                {
                    TotalCount = deliveryPromptModels.Count(),
                    PageSize = pageSize,
                    Data = deliveryQuery.OrderBy(c => c.DeliveryPromptId)
                                        .Skip(pageSize * (pageIndex - 1))
                                        .Take(pageSize)
                                        .ToList(),
                };

                return queryResult;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
